package com.gec.wiki.controller;

import com.gec.wiki.pojo.Category;
import com.gec.wiki.pojo.req.CategoryQueryReq;
import com.gec.wiki.pojo.req.CategorySaveReq;
import com.gec.wiki.pojo.resp.CategoryQueryResp;
import com.gec.wiki.pojo.resp.CommonResp;
import com.gec.wiki.pojo.resp.PageResp;
import com.gec.wiki.service.CategoryService;
import com.gec.wiki.utils.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/getCategoryListByPage")
    public CommonResp getCategoryListByPage(CategoryQueryReq req){
        PageResp<CategoryQueryResp> pageResp = categoryService.getCategoryListByPage(req);

        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
        resp.setContent(pageResp);

        return resp;
    }


    @GetMapping("/allList")
    public CommonResp allList(CategoryQueryReq req){
        List<CategoryQueryResp> resp = categoryService.allList(req);
        CommonResp<List<CategoryQueryResp>> listCommonResp = new CommonResp<>();
        listCommonResp.setContent(resp);
        return listCommonResp;
    }

    @PostMapping("/save")
    public CommonResp save(@RequestBody CategorySaveReq req){
        Category category = CopyUtil.copy(req, Category.class);
        //下面这个方法会根据req对象有无id去分是保存还是修改，没有id就是 save 有id update
        categoryService.saveOrUpdate(category);
        CommonResp<Object> resp = new CommonResp<>();
        return  resp;
    }

    @GetMapping("/remove/{id}")
    public CommonResp remove(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        categoryService.removeById(id);
        CommonResp<Object> resp = new CommonResp<>();
        return resp;
    }

}
